/*
 * BookTest.java
 *
 * This program demonstrates the use of strings to store book names and
 * author names in an array of objects.
 *

 */

package com.javacore.day4.bookrepository;

import java.util.Scanner;

/**
 * This class uses the Book class to create a repository of books.
 *
 * @author 
 */
public class BookTest {
    
    /**
     * An array of objects of class Book to store information about various
     * books. This instance variable acts as a repository of books.
     */
    Book[] books;
    
    /**
     * Instance variable to store the number of books added to the repository.
     */
    byte bookCount;
    
    /**
     * Creates an instance of BookTest class.
     */
    public BookTest() {
        
        bookCount = 0;
    }
    
    /**
     * Method to add a book to the repository.
     */
    void addBook() {
        
        Scanner input = new Scanner(System.in);
        
        // Add a book only if there is space in the array.
        if(bookCount < books.length) {
            
            // Initialize the array element
            books[bookCount] = new Book();
            
            // Accept book name
            System.out.println("Enter book name:");
            books[bookCount].name = input.nextLine();
            
            // Accept author name
            System.out.println("Enter author name:");
            books[bookCount].authorName = input.nextLine();
            
            // Accept price of the book
            System.out.println("Enter book price:");
            books[bookCount].price = input.nextFloat();
            
            // Accept ISBN number of book
            System.out.println("Enter ISBN of the book:");
            books[bookCount].isbn = input.nextInt();
            
            // Accept the year of publication
            System.out.println("Enter year of publication:");
            books[bookCount].yearPublished = input.nextShort();
            
            // Increment the number of books in the repository
            bookCount++;
            
        } else
            System.out.println("Sorry, Unable to add book");
    }
    
    /**
     * Method to display the column headers to display book information.
     */
    void displayHeader() {
        
        System.out.format("%1$-10s %2$-25s %3$-25s %4$-10s %5$-10s",
                "ISBN", "Book Name", "Author Name", "Price($)", "Year of " +
                "Publication");
    }
    
    /**
     * Method to search the books in the repository by book name.
     */
    void searchByBookName() {
        
        Scanner input = new Scanner(System.in);
        String bookName = "";
        
        // Variable to indicate if the search was successful
        boolean bookFound = false;
        
        // Search books only if the repository is not empty
        if(bookCount > 0) {
            
            System.out.println("Enter a book name to search for:");
            bookName = input.nextLine();
            
            displayHeader();
            System.out.print("\n------------------------------------------" +
                    "--------------------------------------------------");
            
            // Iterate until all the books matching the book name are found
            for(int rowIndex = 0;rowIndex < bookCount;rowIndex++){
                
                if(books[rowIndex].name.indexOf(bookName) != -1){
                    
                    System.out.format("\n%1$-10d %2$-25s %3$-25s %4$-10.2f %5$-10d",
                            books[rowIndex].isbn, books[rowIndex].name,
                            books[rowIndex].authorName, books[rowIndex].price,
                            books[rowIndex].yearPublished);
                    bookFound = true;
                }
            }
            // Display a message if a book is not found
            if(!bookFound)
                System.out.println("\nBook not found.");
            
        } else
            System.out.println("\nNo books to search");
    }    
    
    /**
     * Method to display information about all the books in the repository.
     */
    void displayBooks() {
        
        // Display books only if the repository is not empty
        if(bookCount > 0) {
            
            displayHeader();
            System.out.print("\n------------------------------------------" +
                    "--------------------------------------------------");
            
            // Iterate until all the book details are displayed
            for(int rowIndex = 0;rowIndex < bookCount;rowIndex++){
                System.out.format("\n%1$-10d %2$-25s %3$-25s %4$-10.2f %5$-10d",
                        books[rowIndex].isbn, books[rowIndex].name,
                        books[rowIndex].authorName, books[rowIndex].price,
                        books[rowIndex].yearPublished);
            }
        }
    }
    
    /**
     * This is the entry point of the application.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        // Accept the maximum number of books that the repository should store
        byte numberOfBooks = Byte.parseByte(args[0]);
        
        // Create an instance of BookTest class
        BookTest objRepository = new BookTest();
        
        // Create a repository of books to store n books where n is the number
        // specified by varibale numberOfBooks.
        objRepository.books = new Book[numberOfBooks];
        
        Scanner input = new Scanner(System.in);
        
        // Variable to store the option selected by the user
        int choice;
        
        // Iterate until the user chooses to exit
        do {
            System.out.println("\nWelcome to E-Repository.\n" +
                    "Select the operation you want to perform:");
            System.out.println("******************************************");
            
            System.out.println("1. Add book to the repository");
            System.out.println("2. Search book by book name");            
            System.out.println("3. Display books");
            System.out.println("4. Exit");
            System.out.println("******************************************");
            choice = input.nextInt();
            
            // Invoke various methods depending on the operation
            // selected by the user
            switch(choice){
                case 1:
                    objRepository.addBook();
                    break;
                case 2:
                    objRepository.searchByBookName();
                    break;                
                case 3:
                    objRepository.displayBooks();
                    break;
                case 4:
                    break;
                default:
                    System.out.println("Not a valid choice");
            }
        } while(choice != 4);        
    }    
}
